<?php

class PowerController extends Admincontroller
{
    public function init()
    {
        parent::init();
        $this->_power = new PowerModel();
    }

    public function listAction()
    {
        $this->_view->assign("title", '权限管理');
        $this->_view->assign("activetitle", '权限管理');
        $this->_view->assign("parentmenu", '系统管理');
        $this->_view->assign("action", '权限管理');

        if ($this->getRequest()->isPost()) {
            $search = $this->getRequest()->getPost('search');
            $limit = $this->getRequest()->getPost('limit');
            $offset = $this->getRequest()->getPost('offset');

            $userdata = $this->_power->selectAll($offset, $limit, $search);
            $returnarray = array(
                        "total"=> $userdata['0'],
                        "rows" => isset($userdata['1'])?$userdata['1']:[]
                    );
            exit ( json_encode($returnarray) );
        }
        $this->_view->display('Power/list');
    }

    public function addAction()
    {
        $this->_view->assign("title", '新增权限');
        $this->_view->assign("activetitle", '权限管理');
        $this->_view->assign("parentmenu", '系统管理');
        $this->_view->assign("action", '新增权限');
        
        if ($this->getRequest()->isPost()) {
            //校验数据
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('title')->minLength(4)->maxLength(32);
            $validator->field('controller')->minLength(2)->maxLength(16);
            $validator->field('action')->minLength(2)->maxLength(16);
            
            if($validator->validate())
            {
                $info['parentid'] = $this->getRequest()->getPost('parentid');
                $info['title'] = $this->getRequest()->getPost('title');
                $info['controller'] = $this->getRequest()->getPost('controller');
                $info['action'] = $this->getRequest()->getPost('action');
                $info['priority'] = $this->getRequest()->getPost('priority');
                $info['type'] = $this->getRequest()->getPost('type');
                
                //插入数据
                $return = $this->_power->insert($info);
                if ($return) {
                    $returnarray = array('status' => '1', 'data' => '新增权限成功' );
                    exit ( json_encode($returnarray) );
                } else {
                    $returnarray = array('status' => '0', 'data' => '新增权限失败' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                //校验不通过，输出校验数据的结果
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        $result = $this->_power->getallpower();
        $tree = new Tree();
        $str = "<option value='\$id'>\$spacer \$title</option>";
        $tree->init($result);
        $power = $tree->get_tree(0, $str);
        $this->_view->assign("power", $power);
        
        $this->_view->display('Power/add');
    }

    public function editAction()
    {
        $this->_view->assign("title", '编辑权限');
        $this->_view->assign("activetitle", '权限管理');
        $this->_view->assign("parentmenu", '系统管理');
        $this->_view->assign("action", '编辑权限');

        if ($this->getRequest()->isPost()) {
            //校验数据
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('title')->minLength(4)->maxLength(32);
            $validator->field('controller')->minLength(2)->maxLength(16);
            $validator->field('action')->minLength(2)->maxLength(16);
            
            if($validator->validate())
            {
                $info['parentid'] = $this->getRequest()->getPost('parentid');
                $info['title'] = $this->getRequest()->getPost('title');
                $info['controller'] = $this->getRequest()->getPost('controller');
                $info['action'] = $this->getRequest()->getPost('action');
                $info['priority'] = $this->getRequest()->getPost('priority');
                $info['type'] = $this->getRequest()->getPost('type');
                $info['status'] = $this->getRequest()->getPost('status');
                $info['id'] = $this->getRequest()->getPost('id');
                
                //插入数据
                $return = $this->_power->update($info);
                if ($return) {
                    $returnarray = array('status' => '1', 'data' => '编辑权限成功' );
                    exit ( json_encode($returnarray) );
                } else {
                    $returnarray = array('status' => '0', 'data' => '编辑权限失败' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                //校验不通过，输出校验数据的结果
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        
        // 获取指定id的权限信息
        $id = $this->getRequest()->get('id');
        $data = $this->_power->find($id);
        if($data){
            $this->_view->assign("data", $data);
            
            // 获取权限列表信息
            $result = $this->_power->getallpower();
            foreach ($result as $r) {
                $r['selected'] = $r['id'] == $data->parentid ? 'selected' : '';
                $array[] = $r;
            }
            $tree = new Tree();
            $str = "<option value='\$id' \$selected>\$spacer \$title</option>";
            $tree->init($array);
            $power = $tree->get_tree(0, $str);
            $this->_view->assign("power", $power);
            
            $this->_view->display('power/edit');
        }else{
            $this->_view->assign("msg", '指定要编辑的权限不存在');
            $this->_view->display('Public/error');
        }
    }
}
